package com.example.audioreceiver;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;

import java.io.DataInputStream;

public class AudioTrackManager {
    private AudioTrack mAudioTrack;
    private DataInputStream mDis;
    private Thread mRecordThread;
    private boolean isStart = false;
    private volatile static AudioTrackManager mInstance;

    private static final int mStreamType = AudioManager.STREAM_MUSIC;
    private static final int mSampleRateInHz = 96000;
    private static final int mChannelConfig = AudioFormat.CHANNEL_IN_STEREO;
    private static final int mAudioFormat = AudioFormat.ENCODING_PCM_16BIT;
    private static int mMode = AudioTrack.MODE_STREAM;

    private int mMinBufferSize;

    public AudioTrackManager() {
        initData();
    }

    private void initData() {
        mMinBufferSize = AudioTrack.getMinBufferSize(mSampleRateInHz, mChannelConfig, mAudioFormat);
        mAudioTrack = new AudioTrack(mStreamType, mSampleRateInHz, mChannelConfig,
                mAudioFormat, mMinBufferSize, mMode);
    }

    public static AudioTrackManager getInstance() {
        if (mInstance == null) {
            synchronized (AudioTrackManager.class) {
                if (mInstance == null) {
                    mInstance = new AudioTrackManager();
                }
            }
        }
        return mInstance;
    }

    public AudioTrack getAudioTrack(){
        return mAudioTrack;
    }

}
